﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Meteorite;
using System.Diagnostics;

namespace Meteorite
{
    public class ControlPad : CCSprite, ICCTargetedTouchDelegate
    {
        CCSprite Sprbutton;
        

        CCPoint TouchPt = CCPoint.Zero;
        CCPoint CenterPt = null;
        CCPoint Offset = CCPoint.Zero;
        float Radius = 0;
        float SquareRadius = 0;
        bool Pressed;
        
      

        public ControlPad()
        {
            base.initWithFile(GameConstant.ControlPannelBkg);

            CenterPt = new CCPoint(Texture.getContentSize().width / 2, Texture.getContentSize().height / 2);
            //CenterPt = new CCPoint(GameConstant.JoyStick_X, GameConstant.JoyStick_Y);
            

            
        }
        public override void onEnter()
        {
            CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0, true);
            base.onEnter();

            anchorPoint = new CCPoint(0.5f, 0.5f);
            position = new CCPoint(GameConstant.JoyStick_X, GameConstant.JoyStick_Y);


            Sprbutton = CCSprite.spriteWithFile(GameConstant.ControlPannelBtn);
            Sprbutton.anchorPoint = new CCPoint(0.5f, 0.5f);
            Sprbutton.position = new CCPoint(CenterPt.x, CenterPt.y);
            addChild(Sprbutton);

            Pressed = false;
            Radius = Texture.getContentSize().width / 2;
            SquareRadius = Radius * Radius;
        }

        public override void onExit()
        {
            CCTouchDispatcher.sharedDispatcher().removeDelegate(this);
            base.onExit();
        }

        bool IsPointInPad(CCPoint Pt)
        {
            //float DisSquare = (Pt.x - CenterPt.x) * (Pt.x - CenterPt.x) + (Pt.y - CenterPt.y) * (Pt.y - CenterPt.y);
            float DisSquare = (Pt.x - GameConstant.JoyStick_X) * (Pt.x - GameConstant.JoyStick_X) +
                (Pt.y - GameConstant.JoyStick_Y) * (Pt.y - GameConstant.JoyStick_Y);
            return DisSquare <= SquareRadius;
        }
        public void Loop()
        {
            if (!Pressed /*&& IsPointInPad(TouchPt)*/)
            {
                Sprbutton.position = CenterPt;
                Offset = CCPoint.Zero;
                return;
            }

            Offset.x = TouchPt.x - CenterPt.x-Texture.getContentSize().width / 2;
            Offset.y = TouchPt.y - CenterPt.y- Texture.getContentSize().height / 2;

            double angle = Math.Atan2((double)Offset.y , (double)Offset.x);
            float qrt = Offset.x * Offset.x + Offset.y * Offset.y;
            float len = (float)Math.Sqrt((double)(qrt));
            len = Math.Min(len, Radius);

            Offset.x = len * (float)Math.Cos(angle);
            Offset.y = len * (float)Math.Sin(angle);

            CCPoint pt = new CCPoint(CenterPt.x+Offset.x, CenterPt.y+Offset.y);
            //CCPoint pt = new CCPoint( Offset.x, Offset.y);

            Sprbutton.position = pt;
        }

        public virtual bool ccTouchBegan(CCTouch touch, CCEvent eventer)
        {
            CCPoint touchPoint = touch.locationInView(touch.view());
            TouchPt = CCDirector.sharedDirector().convertToGL(touchPoint);
            if (IsPointInPad(TouchPt))
            {
                Pressed = true;
            }
            else
            {
                Pressed = false;
            }
            return true;
        }

        public virtual void ccTouchMoved(CCTouch touch, CCEvent eventer)
        {
            if (!Pressed)
            {
                return;
            }
            CCPoint touchPoint = touch.locationInView(touch.view());
            TouchPt = CCDirector.sharedDirector().convertToGL(touchPoint);
        }

        public virtual void ccTouchEnded(CCTouch touch, CCEvent eventer)
        {
            Pressed = false;
            
        }
        public void ccTouchCancelled(CCTouch pTouch, CCEvent pEvent)
        {
            Pressed = false;
        }
        public CCPoint GetOffsetPercent()
        {
            return new CCPoint(Offset.x / Radius, Offset.y / Radius);
        }
    }
}
